package com.lanhe.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.product.domain.LhQingchuangProductDetails;
import com.lanhe.product.mapper.LhQingchuangProductDetailsMapper;
import com.lanhe.product.service.ILhQingchuangProductDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 清创产品详情(LhQingchuangProductDetails)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-20 12:51:14
 */
@Service
public class LhQingchuangProductDetailsServiceImpl extends ServiceImpl<LhQingchuangProductDetailsMapper, LhQingchuangProductDetails> implements ILhQingchuangProductDetailsService {

	@Autowired
	private LhQingchuangProductDetailsMapper productDetailsMapper;

	/**
	 * 更新产品详情数据
	 *
	 * @param productDetails 实体
	 * @author zhanggl
	 * @date 2023/12/20
	 */
	@Override
	public LhQingchuangProductDetails saveProductDetails(LhQingchuangProductDetails productDetails) {
		LhQingchuangProductDetails productDetailsRet = getProductDetailsByIdAndNumber(productDetails.getId(), productDetails.getNumber());
		if (ObjectUtil.isNotEmpty(productDetailsRet)){
			productDetailsMapper.updateById(productDetails);
		} else {
			productDetailsMapper.insert(productDetails);
		}
		return productDetails;
	}

	/**
	 * 根据ID和编号查询数据
	 *
	 * @param id ID
	 * @param number 编号
	 * @return com.lanhe.product.domain.LhQingchuangProductDetails
	 * @author zhanggl
	 * @date 2023/12/20
	 */
	private LhQingchuangProductDetails getProductDetailsByIdAndNumber(Long id, String number) {
		LambdaQueryWrapper<LhQingchuangProductDetails> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhQingchuangProductDetails::getId, id);
		queryWrapper.eq(LhQingchuangProductDetails::getNumber, number);
		return productDetailsMapper.selectOne(queryWrapper);
	}

	/**
	 * 查询详情数据
	 *
	 * @param number 产品编号
	 * @return com.lanhe.product.domain.LhQingchuangProduct
	 * @author zhanggl
	 * @date 2023/12/20
	 */
	@Override
	public LhQingchuangProductDetails selectProductDetailByNumber(String number) {
		LambdaQueryWrapper<LhQingchuangProductDetails> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhQingchuangProductDetails::getNumber, number);
		return productDetailsMapper.selectOne(queryWrapper);
	}
}

